package com.juzipi.rabbit.consumer;

import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * @Author juzipi
 * @Date 2021/7/29 15:49
 * @Info
 */
@Component
@RabbitListener(queues = "demo.queue")
public class DemoConsumer {

    @RabbitHandler
    public void process(String messageString, Channel channel, Message message) throws IOException {
        try {
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
            System.out.println("监听demoQueue队列获取到的消息:");
            System.out.println(messageString);
            System.out.println("-----------------");
        } catch (Exception e) {
            e.printStackTrace();
            channel.basicReject(message.getMessageProperties().getDeliveryTag(),true);
        }

    }


}
